<?php
/**
 * MySQLi 实现伪 pdo_mysql  
 *
 * @filesource mmysqli.class.php
 * @package MagicDB 
 * @subpackage 
 * @version $id: 0.1, utf8, Sun Dec 20 21:18:39 CST 2009
 * @author LD King <kldscs[at]gmail.com>
 * @copyright Copyleft (D.) 2007 - 2009 MiFunny China Inc.
 * @link http://mifunny.info/
 * @example 
 *   
 */
class mmysqli implements MagicDB{
    private $mysqli = null;

    /**
     * 初始化 MySQLi
     *
     * @param array $policy
     */
    public function __construct($policy){
        if( empty($policy) ){
            throw new RuntimeException('('.__CLASS__.'->'.__FUNCTION__.')'.
                'Error: empty database policy array !');
        }else{
            $this->mysqli = new mysqli($policy['host'], $policy['username'], $policy['password'], $policy['dbname'], $policy['unix_socket']);
            if($this->mysqli->connect_error) {
                throw new LogicException('('.__CLASS__.'->'.__FUNCTION__.')'.
                    'MySQL Connect Error: #'.$this->mysqli->connect_errno . ', '.$this->mysqli->connect_error);
            }else{
                if( !empty($policy['charset']) ){
                    $charset = 'SET NAMES '.$policy['charset'];
                    if( !empty($policy['collate']) ){
                        $charset .= ' COLLATE '.$policy['collate'];
                    }
                    $this->exec( $charset );
                }//END set charset
            }
        }
    }//END func __construct

    public function __destruct(){
        if( $this->mysqli instanceof mysqli ){
            $this->mysqli->close();
        }
    }

    /**
     * 错误代码
     *
     * @return mixed
     */
    public function errorCode(){
        return mysqli_connect_errno();
    }

    /** 
     * 错误性息
     *
     * @return array
     */
    public function errorInfo(){
        return mysqli_connect_error();
    }

    /**
     * 执行SQL语句
     *
     * @param string $statement
     *
     * @return int
     */
    public function exec($statement){
        if( $this->mysqli->query($statement) ){
            return true;
        }else{
            throw new LogicException('('.__CLASS__.'->'.__FUNCTION__.')'.
                'SQL execute Error: #'.$this->errorCode().', '.$this->errorInfo() );
            return false;
        }
    }

    /**
     * 执行SQL语句,返回结果集
     *
     * @param string $statement
     *
     * @return MagicStatement
     */
    public function query($statement){
        if( $stmt = $this->mysqli->query($statement) ){
            return new mmysqliStatement($this->mysqli, $stmt); 
        }else{
            throw new LogicException('('.__CLASS__.'->'.__FUNCTION__.')'.
                'SQL execute Error: #'.$this->errorCode().', '.$this->errorInfo() );
        }
    }

    /**
     * 得到最近一次insert的ID
     *
     * @return string
     */
    public function lastInsertId(){
        return $this->mysqli->insert_id;
    }
    
    /**
     * 去除SQL语句中不合法的字符
     *
     * @param string $string
     *
     * @return string
     */
    public function quote($string){
        return $thsi->mysqli->real_escape_string($string);
    }

    /**
     * 开始事务
     */
    public function beginTransaction(){
        $this->mysqli->autocommit(FALSE);
    }

    /**
     * 提交事务
     */
    public function commit(){
        $this->mysqli->commit();
    }

    /**
     * 回滚事务
     */
    public function rollBack(){
        $this->mysqli->rollback();
    }

}//END interface MagicDB

/**
 * MagicDB 结果集
 */
class mmysqliStatement implements MagicStatement{
    private $mysqli =null;
    private $stmt = null;

    /**
     * 结果期集
     *
     * @param MySQLi $mysqli
     * @param MySQLi_Result $stmt
     */
    public function __construct($mysqli, $stmt){
        $this->mysqli = $mysqli;
       $this->stmt = $stmt; 
    }

    /**
     * 获得单行结果
     *
     * @return mixed
     */
    public function fetch(){
        return $this->stmt->fetch_assoc();
    }

    /**
     * 获取所有结果
     *
     * @return array
     */
    public function fetchAll(){
        while( $row = $this->stmt->fetch_assoc() ) {
            $result[] = $row;
        }
        return $result;
    }

    /**
     * 结果计数
     *
     * @return int
     */
    public function rowCount(){
        return $this->stmt->num_rows;
    }

    /**
     * 下一个结果集
     *
     * @return bool
     */
    public function nextRowset(){
        $this->closeCursor();
        $this->mysqli->next_result();
    }

    /**
     * 关闭结果集
     *
     * @return bool
     */
    public function closeCursor(){
        $this->stmt->free();
    }
}//END class mmysqliStatement
?>
